package com.example.teesystem.controller;

import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.PageRequestVo;
import com.example.teesystem.entity.vo.exam.ExamUpdateVo;
import com.example.teesystem.entity.vo.examEvaluation.EvaluationUpdateVo;
import com.example.teesystem.entity.vo.examEvaluation.ExamEvaluationInsertVo;
import com.example.teesystem.entity.vo.examPaper.*;
import com.example.teesystem.entity.vo.issue.IssueSelectVo1;
import com.example.teesystem.service.ExamEvaluationService;
import com.example.teesystem.service.ExamPaperService;
import com.example.teesystem.service.IssueService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/4/11 9:15
 */
@RestController
@RequestMapping(value = "/tee/examPaper" , produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor
public class ExamPaperController {
    private final ExamPaperService examPaperService;
    private final IssueService issueService;
    private final ExamEvaluationService examEvaluationService;
    /**
     * 添加试卷
     * @param examPaperInsertVo
     * @return
     */
    @PostMapping(value = "/addExamPaper")
    @PermissionCheck(PermissionUtil.PAPER_PROVIDE)
    public Response addExamPaper(@Valid @RequestBody(required = false)ExamPaperInsertVo examPaperInsertVo){
        return examPaperService.addExamPaper(examPaperInsertVo);
    }

    /**
     * 教师端分页查询试卷
     * @param examPaperSelectVo
     * @return
     */
    @PostMapping(value = "/getExamPaper")
    public Response getExamPaper(@Valid @RequestBody ExamPaperSelectVo examPaperSelectVo){
        return examPaperService.getExamPaperList(examPaperSelectVo);
    }

    /**
     * 删除试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/deleteExamPaper")
    public Response deleteExamPaper(@RequestBody Map<String, String> map){
        int id;
        try{
            id = Integer.parseInt(map.get("id"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return examPaperService.deleteExamPaper(id);
    }

    /**
     * 更新试卷
     * @param examPaperUpdateVo1
     * @return
     */
    @PostMapping(value = "/updateExamPaper")
    public Response updateExamPaper(@Valid @RequestBody ExamPaperUpdateVo1 examPaperUpdateVo1){
        return examPaperService.updateExamPaper(examPaperUpdateVo1);
    }

    /**
     * 根据课程id、题型、题库获取题目列表
     * @param issueSelectVo1
     * @return
     */
    @PostMapping(value = "/getIssue")
    public Response getIssue(@Valid @RequestBody IssueSelectVo1 issueSelectVo1){
        return issueService.getIssueByMultiConditionInExamDataBase(issueSelectVo1);
    }

    /**
     * 预览试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/previewExamPaper")
    public Response previewExamPaper(@RequestBody Map<String, String> map){
        int id;
        try{
            id = Integer.parseInt(map.get("id"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return examPaperService.previewExamPaper(id);
    }

    /**
     * 智能组卷
     */
    @ApiOperation("智能组卷")
    @PostMapping(value = "/intelligentTestPaperGeneration")
    @PermissionCheck(PermissionUtil.PAPER_PROVIDE)
    public Response intelligentTestPaperGeneration(@Valid @RequestBody(required = false) IntelligentPaperCreateVo intelligentPaperCreateVo){
        return examPaperService.getRandomIssue(intelligentPaperCreateVo);
    }

    /**
     * 获取试卷的重复率
     * @param map
     * @return
     */
    @PostMapping(value = "/getPaperRepetitionRate")
    public Response getPaperRepetitionRate(@RequestBody Map<String, String> map){
        int currentPaperId, selectPaperId;
        try {
            currentPaperId = Integer.parseInt(map.get("currentPaperId"));
            selectPaperId = Integer.parseInt(map.get("selectPaperId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.getPaperRepetitionRate(currentPaperId, selectPaperId);
    }

    /**
     * 发布考试
     * @param
     * @return
     */
    @PostMapping(value = "/releaseExamPaper")
    public Response releaseExamPaper(@RequestBody Map<String,Integer> map){
        Integer paperId = map.get("paperId");
        if(paperId == null) {
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.releaseExamPaper(paperId);
    }

    /**
     * 撤销发布的试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/undoExamPaper")
    public Response undoExamPaper(@RequestBody Map<String, String> map){
        int id;
        try{
            id = Integer.parseInt(map.get("id"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return examPaperService.undoExamPaper(id);
    }

    /**
     * 学生端 获取考试试卷列表
     * @return
     */
    @PostMapping(value = "/studentGetExamList")
    public Response studentGetExamList(@Valid @RequestBody PageRequestVo pageRequestVo){
        return examPaperService.studentGetExamList(pageRequestVo);
    }

    /**
     * 获取未阅试卷列表,根据试卷，课程，班级
     * @param examPaperSelectVo1
     * @return
     */
    @PostMapping(value = "/getNotMarkPaperList")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response getNotMarkPaperList(@Valid @RequestBody ExamPaperSelectVo1 examPaperSelectVo1){
        return examEvaluationService.getNotMarkExamPaper(examPaperSelectVo1);
    }
//
//    /**
//     * 根据课程、考试获取获取学生试卷列表
//     * @param examPaperSelectVo1
//     * @return
//     */
//    @PostMapping(value = "/getNotMarkPaperList")
//    @PermissionCheck(PermissionUtil.PAPER_READ)
//    public Response getNotMarkPaperList1(@Valid @RequestBody ExamPaperSelectVo1 examPaperSelectVo1){
//        return examEvaluationService.getNotMarkExamPaper(examPaperSelectVo1);
//    }

    /*
    * 根据班级id获取未阅试卷列表
    * @param
    * @return
    * */
    @PostMapping(value = "/getNotMarkPaperByClassId")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response getNotMarkPaperByClassId(@Valid @RequestBody ExamPaperSelectVo1 examPaperSelectVo1) {
        return examEvaluationService.getNotMarkPaperByClassId(examPaperSelectVo1);
    }



    /**
     * 完成单个学生阅卷
     * @param examEvaluationInsertVo
     * @return
     */
    @PostMapping(value = "/finishMarkSingleStudentPaper")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response finishMarkSingleStudentPaper(@Valid @RequestBody ExamEvaluationInsertVo examEvaluationInsertVo){
        return examPaperService.finishMarkSingleStudentPaper(examEvaluationInsertVo);
    }
    
    @PostMapping(value = "/saveUnMarkedPaper")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response saveUnMarkedPaper(@RequestBody ExamEvaluationInsertVo examEvaluationInsertVo){
        return examPaperService.saveUnMarkedPaper(examEvaluationInsertVo);
    }

    /**
     * 获取已阅试卷列表
     * @param examPaperSelectVo1
     * @return
     */
    @PostMapping(value = "/getMarkPaperList")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response getMarkPaperList(@Valid @RequestBody ExamPaperSelectVo1 examPaperSelectVo1){
        return examEvaluationService.getMarkExamPaper(examPaperSelectVo1);
    }

    /**
     * 获取已阅试卷简要列表
     * @param examPaperSelectVo1
     * @return
     */
    @PostMapping(value = "/getMarkPaperSimpleList")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response getMarkPaperSimpleList(@Valid @RequestBody ExamPaperSelectVo1 examPaperSelectVo1){
        return examEvaluationService.getMarkPaperSimpleList(examPaperSelectVo1);
    }

    /**
     * 获取已阅试卷详情
     * @param map
     * @return
     */
    @PostMapping(value = "/getMarkPaperMessage")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response getMarkPaperMessage(@RequestBody Map<String, String> map){
        int paperId;
        int studentId;
        try{
            paperId = Integer.parseInt(map.get("paperId"));
            studentId = Integer.parseInt(map.get("studentId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return examPaperService.getMarkedPaperMessage(paperId, studentId);
    }

    /**
     * 修改已阅试题
     * @param examUpdateVo
     * @return
     */
    @PostMapping(value = "/updateMarkedIssue")
    @PermissionCheck(PermissionUtil.PAPER_READ)
    public Response updateMarkedIssue(@Valid @RequestBody ExamUpdateVo examUpdateVo){
        return examPaperService.updateMarkedIssue(examUpdateVo);
    }

    /**
     * 修改试卷评价
     * @param evaluationUpdateVo
     * @return
     */
    @PostMapping(value = "/updateExamPaperMessage")
    @PermissionCheck(PermissionUtil.PAPER_RELEASE)
    public Response updateExamPaperMessage(@Valid @RequestBody EvaluationUpdateVo evaluationUpdateVo){
        return examPaperService.updateExamPaperMessage(evaluationUpdateVo);
    }

    /**
     * 发布已阅试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/publishMarkedPaper")
    @PermissionCheck(PermissionUtil.PAPER_RELEASE)
    public Response publishMarkedPaper(@RequestBody Map<String, String> map){
        int classId;
        int paperId;
        try{
            classId = Integer.parseInt(map.get("classId"));
            paperId = Integer.parseInt(map.get("paperId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        return examPaperService.publishMarkedPaper(classId, paperId);
    }

    /**
     * 撤销已发布的学生试卷
     * @param map
     * @return
     */
    @PostMapping(value = "/unPublishExamPaper")
    @PermissionCheck(PermissionUtil.PAPER_RELEASE)
    public Response unPublishExamPaper(@RequestBody Map<String, String> map){
        int paperId;
        int studentId;
        try {
            paperId = Integer.parseInt(map.get("paperId"));
            studentId = Integer.parseInt(map.get("studentId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.unPublishExamPaper(paperId, studentId);
    }

    /**
     * 学生获取已阅完试卷列表
     * @param pageRequestVo
     * @return
     */
    @PostMapping(value = "/studentGetMarkedPaperList")
    public Response studentGetMarkedPaperList(@Valid @RequestBody PageRequestVo pageRequestVo){
        return examEvaluationService.studentGetMarkedPaperList(pageRequestVo);
    }

    /**
     * 学生获取已阅试卷详情
     * @param map
     * @return
     */
    @PostMapping(value = "/studentGetMarkedPaperMessage")
    public Response studentGetMarkedPaperMessage(@RequestBody Map<String, String> map){
        int paperId;
        try {
            paperId = Integer.parseInt(map.get("paperId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.studentGetMarkedPaperMessage(paperId);
    }

    /**
     * 根据课程id获取试卷列表(下拉框)
     * @param map
     * @return
     */
    @PostMapping(value = "/getPaperMessageList")
    public Response getPaperMessageList(@RequestBody Map<String, String> map){
        int courseId;
        try {
            courseId = Integer.parseInt(map.get("courseId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.getPaperMessageList(courseId);
    }

    /**
     * 获取历史试卷列表（下拉框）
     * @return
     */
    @PostMapping(value = "/getHistoryPaperList")
    public Response getHistoryPaperList(){
        return examPaperService.getHistoryPaperList();
    }

    /**
     * 根据课程id获取已阅试卷列表(下拉框)
     * @param map
     * @return
     */
    @PostMapping(value = "/getMarkedPaperMessageList")
    public Response getMarkedPaperMessageList(@RequestBody Map<String, String> map){
        int courseId;
        try {
            courseId = Integer.parseInt(map.get("courseId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return examPaperService.getMarkedPaperMessageList(courseId);
    }

    @PostMapping(value = "/finishExam")
    public Response finishExam(@RequestBody Map<String,Integer> map){
        Integer paperid = map.get("paperId");
        if(paperid == null) {
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        try{
            return examPaperService.finishExamByTeacher(paperid);
        }catch (Exception e){
            return Response.error(ResultCode.UPDATE_MESSAGE_ERROR);
        }
    }

    /**
     * 预测试卷分数
     * @param map
     * @return
     */
    @PostMapping(value = "/forecastScore")
    public Response preScore(@RequestBody Map<String,Integer> map){
        Integer paperid = map.get("paperId");
        if(paperid == null) {
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        try{
            return examPaperService.forecastScore(paperid);
        }catch (Exception e){
            return Response.error(ResultCode.GET_ERROR);
        }
    }

    /**
     * 试卷题目替换
     * @param examPaperInstead
     * @return
     */
//    @PostMapping(value = "/examPaperInstead")
//    public Response examPaperInstead(@RequestBody ExamPaperInstead examPaperInstead){
//
//    }
}
